package com.shop.controller;

import com.shop.annotation.RequireRoles;
import com.shop.config.MiniAppLoginRequest;
import com.shop.entity.CommonUser;
import com.shop.model.PageParam;
import com.shop.model.vo.Rsp;
import com.shop.service.UserService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;

@Validated
@RestController
public class UserController {
    @Resource
    private UserService userService;

    // @RedisLock(lockName = "login", key = "#user.id+':'+#user.account")
    @PostMapping("login")
    public Rsp login(@RequestBody CommonUser user) throws InterruptedException {
        return userService.login(user);
    }

    /**
     * 登出方法
     *
     * @return
     */
    @GetMapping("logout")
    public Rsp logout(@NotNull(message = "token不能为空") String token) {
        return userService.logout(token);
    }

    @GetMapping("getUserInfo")
    public Rsp getUserInfo(@NotNull(message = "token不能为空") String token) {
        return userService.getUserInfo(token);
    }

    @PostMapping("miniAppLogin")
    public Rsp miniAppLogin(@RequestBody MiniAppLoginRequest code) {
        return userService.miniAppLogin(code);
    }

    @GetMapping("getMerchantId")
    public Rsp getMerchantId(/*@RequestHeader("userId")*/ Integer userId) {
        return userService.getMerchantId(userId);
    }

    /**
     * 在线踢人
     */
    @RequireRoles(roles = {"platform"})
    @PostMapping("kickOut")
    public Rsp kickOutById(@RequestBody CommonUser user) {
        return userService.kickOutById(user);
    }

    @PostMapping("getAllUser")
    public Rsp getAllUser(@RequestBody PageParam<CommonUser> pageParam){
        return userService.getAllUser(pageParam);
    }

    @PostMapping("updateStatus")
    public Rsp updateStatus(@RequestBody CommonUser user){
        return userService.updateStatus(user);
    }
}



